<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *智能体
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$tabs=array(
	array('op'=>'list','title'=>'智能体模板列表'),
	array('op'=>'edit','title'=>'添加智能体模板'),
	//array('op'=>'listadd','title'=>'批量导入智能体表'),
);
if($_RQ['op']=='list'){
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	if(!empty($_RQ['id'])){
		$params['id']=intval($_RQ['id']);
		$return['params']['id']=$_RQ['id'];
	}
	if(!empty($_RQ['agetype'])){
		$params['agetype']=intval($_RQ['agetype']);
		$return['params']['agetype']=$_RQ['agetype'];
	}
	if(!empty($_RQ['yintype'])){
		$params['yintype like']='%'.trim($_RQ['yintype']).'%';
		$return['params']['yintype']=$_RQ['yintype'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$list=pdo_getslice('jy_aiagent_agent',$params,array($pindex,$psize),$total,$fields,'','isdemo desc,id desc');
	if($list){
		foreach($list as &$item){
			$item['member']=cfc('core')->member_show($item['mid']);
			$item['voice']=pdo_get('jy_aiagent_voice',array('id'=>$item['voiceid'],'uniacid'=>$_SESSION['uniacid']));
			$item['language']=$item['voice']['language'];
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$tips='这里是平台智能体模板，用户的智能体不在这里展示！';
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','tip'=>$tips,'edit'=>true,'list'=>array(
			array('name'=>'logo','title'=>'头像','type'=>'image'),
			array('name'=>'title','title'=>'智能体名称','type'=>'string'),
			array('name'=>'llm_model','title'=>'模型','type'=>'string'),
			array('name'=>'language','title'=>'语言','type'=>'radio','options'=>'yuyuantypes'),
			array('name'=>'voiceid','title'=>'绑定音色','type'=>'select','options'=>'voices'),
			array('name'=>'default_tts_voice','title'=>'默认voice参数值','type'=>'string'),
			array('name'=>'groupid','title'=>'智能体分类','type'=>'select','options'=>'groups'),
			array('name'=>'desc','title'=>'描述','type'=>'string'),
			array('name'=>'status','title'=>'状态','type'=>'switch','set'=>'set'),
			array('name'=>'createtime','title'=>'时间','type'=>'time'),
		),'batch'=>array(
			array('type'=>'post','url'=>'/agent/updata/','title'=>'同步智能体'),
			//array('type'=>'post','url'=>'/agent/updemo/','title'=>'一键生成试听'),
		)),
		'search'=>array(
			array('name'=>'title','width'=>15,'title'=>'智能体名称','type'=>'string'),
		)
	);
	$return['agetype']=$this->agetype;
	$return['yuyuantypes']=$this->yuyuantypes;
	$return['voices']=pdo_getall('jy_aiagent_voice',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['groups']=pdo_getall('jy_aiagent_agent_group',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['htmls']=$htmls;
	$return['isfen']=array('不支持','支持');
	exi($return);
}
if($_RQ['op']=='edit'){
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_aiagent_agent',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		$item['voice']=pdo_get('jy_aiagent_voice',array('id'=>$item['voiceid'],'uniacid'=>$_SESSION['uniacid']));
		$item['language']=$item['voice']['language'];
	}else{
		$item=array('type'=>$this->session('type'),'star'=>3,'isnews'=>0);
		$item['language']='zh';
	}
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回智能体列表'),
		),
		'edit'=>array(
			array('name'=>'title','title'=>'智能体名称','type'=>'string'),
			array('name'=>'logo','title'=>'智能体头像','type'=>'image'),
			array('name'=>'ainame','title'=>'AI机器人名称','type'=>'string'),
			array('name'=>'user_name','title'=>'主人名称','type'=>'string'),
			array('type'=>'select2_line','name'=>'language','options'=>'yuyuantypes','title'=>'语言','help'=>'',
				'name2'=>'voiceid','options2'=>'voices','title2'=>'音色',
			),
			array('name'=>'groupid','title'=>'主分类','type'=>'select','options'=>'groups'),
			array('name'=>'llm_model','title'=>'语言模型','type'=>'radio','options'=>'llm_models'),
			array('name'=>'desc','title'=>'智能体介绍','type'=>'text'),
			array('name'=>'paixu','title'=>'排序','type'=>'string'),
			array('name'=>'status','title'=>'状态','type'=>'switch'),
		)
	);
	$return['llm_models']=$this->llm_models;
	$return['groups']=pdo_getall('jy_aiagent_agent_group',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$yuyuantypes=$this->yuyuantypes;
	$return['yuyuantypes']=array();
	foreach($yuyuantypes as $k=>$title){
		$return['yuyuantypes'][$k]=array('id'=>$k,'title'=>$title);
	}
	foreach($return['yuyuantypes'] as $k=>&$yv){
		$yv['voices']=pdo_getall('jy_aiagent_voice',array('uniacid'=>$_SESSION['uniacid'],'language'=>$yv['id']),array('title','id'),'id');
	}
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	if(!$postdata['mid']){
		//exi('请选择发布人！','error');
	}
	if(!$postdata['title']){
		exi('请填写智能体名称！','error');
	}
	if(!$postdata['groupid']){
		//exi('请填写智能体分类！','error');
	}
	if(!$postdata['voiceid']){
		exi('请绑定音色！','error');
	}
	if(!$postdata['llm_model']){
		exi('请绑定语言模型！','error');
	}
	$voice=pdo_get('jy_aiagent_voice',array('id'=>$postdata['voiceid'],'uniacid'=>$_SESSION['uniacid']));
	$data=array(
		'title'=>trim($postdata['title']),
		'logo'=>trim($postdata['logo']),
		'desc'=>trim($postdata['desc']),
		'default_tts_voice'=>$voice['language'].':'.$voice['voiceid'],
		'voiceid'=>$postdata['voiceid'],
		'llm_model'=>trim($postdata['llm_model']),
		'ainame'=>trim($postdata['ainame']),
		'user_name'=>trim($postdata['user_name']),
		'groupid'=>intval($postdata['groupid']),
		'status'=>intval($postdata['status']),
		'paixu'=>intval($postdata['paixu']),
	);
	//不需要同步到API
	//$res=$this->model('api')->upagentstpl($data,1);
	$id=intval($postdata['id']);
	if($id){
		pdo_update('jy_aiagent_agent',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		$data['createtime']=TIMESTAMP;
		$data['apiid']=$res['id'];
		pdo_insert('jy_aiagent_agent',$data);
	}
	exi('操作成功！','','list');
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择智能体！','error');
	}
	$post=get_postdata();
	if(isset($post['status'])){
		pdo_update('jy_aiagent_agent',array('status'=>intval($post['status'])),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	exi('操作成功！');
}
if($_RQ['op']=='updata'){
	$post=array(
		'page'=>1,
		'pageSize'=>100,
	);
	$isvoice=pdo_count('jy_aiagent_voice',array('uniacid'=>$_SESSION['uniacid']));
	if($isvoice<3){
		exi('请先同步音色！至少3种以上！','error');
	}
	$list=$this->model('api')->getagentstpl($post,1);
	foreach($list as $item){
		$isadd=pdo_get('jy_aiagent_agent',array('apiid'=>trim($item['id']),'uniacid'=>$_SESSION['uniacid']));
		$default_tts_voice=str_array($item['default_tts_voice'],':');
		$voice=pdo_get('jy_aiagent_voice',array('language'=>$default_tts_voice[0],'voiceid'=>trim($default_tts_voice[1]),'uniacid'=>$_SESSION['uniacid']));
		if(!$voice){
			$voice=pdo_get('jy_aiagent_voice',array('voiceid'=>trim($default_tts_voice[1]),'uniacid'=>$_SESSION['uniacid']));
		}
		$item['logo']=$item['logo']?:$voice['logo'];
		$data=array(
			'apiid'=>trim($item['id']),
			'title'=>trim($item['agent_name']),
			'logo'=>trim($item['logo']),
			'desc'=>trim($item['character']),
			'tts_voices'=>ijson_encode($item['tts_voices']),
			'default_tts_voice'=>trim($item['default_tts_voice']),
			'voiceid'=>$voice['id'],
			'llm_model'=>trim($item['llm_model']),
			'ainame'=>trim($item['assistant_name']),
			'user_name'=>trim($item['user_name']),
			'tts_speech_speed'=>trim($item['tts_speech_speed']),
			'asr_speed'=>trim($item['asr_speed']),
			'tts_pitch'=>trim($item['tts_pitch']),
			'tts_voice_name'=>trim($item['tts_voice_name']),
			'isdemo'=>1,
			'status'=>1,
		);
		$id=intval($isadd['id']);
		if($id){
			unset($data['logo']);
			pdo_update('jy_aiagent_agent',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}else{
			$data['uniacid']=$_SESSION['uniacid'];
			$data['createtime']=TIMESTAMP;
			pdo_insert('jy_aiagent_agent',$data);
		}
	}
	$item=$list[0];
	if($item){
		$default_tts_voice=str_array($item['default_tts_voice'],':');
		$voices=pdo_getall('jy_aiagent_voice',array('language'=>$default_tts_voice[0],'uniacid'=>$_SESSION['uniacid']));
		foreach($voices as $vo){
			$isadd=pdo_get('jy_aiagent_agent',array('default_tts_voice'=>$vo['language'].':'.$vo['voiceid'],'uniacid'=>$_SESSION['uniacid']));
			$num = mt_rand(1, 5);
			$llm_model='qwen';
			if($num==1){
				$llm_model='qwen';
			}elseif($num==2){
				$llm_model='deepseek';
			}elseif($num==3){
				$llm_model='doubao';
			}elseif($num==4){
				$llm_model='qwen3';
			}
			$data=array(
				'apiid'=>'',
				'title'=>trim($vo['title']),
				'logo'=>trim($vo['logo']),
				'desc'=>trim($item['character']),
				'tts_voices'=>ijson_encode($item['tts_voices']),
				'default_tts_voice'=>$vo['language'].':'.$vo['voiceid'],
				'voiceid'=>$vo['id'],
				'llm_model'=>$llm_model,
				'ainame'=>trim($item['assistant_name']),
				'user_name'=>trim($item['user_name']),
				'tts_speech_speed'=>trim($item['tts_speech_speed']),
				'asr_speed'=>trim($item['asr_speed']),
				'tts_pitch'=>trim($item['tts_pitch']),
				'tts_voice_name'=>trim($item['tts_voice_name']),
				'status'=>1,
			);
			$id=intval($isadd['id']);
			if($id){
			}else{
				$data['uniacid']=$_SESSION['uniacid'];
				$data['createtime']=TIMESTAMP;
				pdo_insert('jy_aiagent_agent',$data);
			}
		}
	}
	exi('同步成功！');
}
if($_RQ['op']=='listadd'){
	if($_RQ['postdata']){
		$postdata=get_postdata();
		$this->model('zhubo')->daoru($postdata);
		exi('导入成功','','/agent/');
	}
	$return['item']['excelurl']=SITEROOT.'/addons/jy_aiagent/resource/agent.xls';
	//$return['item']['excelurl']='请直接下载模板选择导入！';
	$htmls=array (
		  'type' => 'edit',
		  'tabs'=>$tabs,
		  'edit' => array (
				array ('type' => 'tip','title' => '<red>[温馨提示]：在导入过程中，请不要进行任何操作!</red></br>
				功能介绍：可将本模块导出的XLSX文件快速上传,节约您的大量时间!</br>
				1、导入时会匹配【姓名】判断重复；</br>
				2、导入时会匹配【voice参数值】判断重复；</br>
				3、导入时会匹配【类型】，如果不存在，将自动添加该分类类型；</br>
				4、重复的会自动忽略；</br>
				5、注意<red>【姓名】，【voice参数值】</red>的准确性！
				
				'),
				array ('name' => 'excelurl','type' => 'copy','title' => 'XLSX示例文件下载'),
				array ('name' => 'excel','type' => 'upfile','title' => '上传XLSX','help'=>''),
		  ),
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_delete('jy_aiagent_agent',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}